<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class ImportH5Menu extends Command
{
    protected function configure()
    {
        $this->setName('importMenu:H5')
            ->addOption('path', null, Option::VALUE_REQUIRED, 'route file path')
            ->setDescription('import H5 route to  Menu');
    }

    protected function execute(Input $input, Output $output)
    {
        if ($input->hasOption('path')) {
            $path = $input->getOption('path');
        } else {
            $path = \think\facade\Env::get('ROOT_PATH') . 'h5.json';
        }
        $router = json_decode(file_get_contents($path), true);
        $model = new \app\admin\model\Menu();
        $list = array_map(function ($r) {
            $o['name'] = $r;
            $o['ua'] = 'h5';
            $o['permission_id'] = 0;
            $o['parent_id'] = 0;
            return $o;
        }, $router);

        try {
            $model->saveAll($list);
            $output->writeln("import, done");
        } catch (\Exception $e) {
            $output->writeln("import, fail");
        }
    }
}
